package com.inspect.protocol;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;

import com.inspect.dbConn.dbSingleton;
import com.inspect.log.LogSingleton;

public class Inspect_record {
	private static Logger log = LogSingleton.get_logger(Inspect_record.class);

	/**
	 * 
	 * @param result：结果
	 * @param status：巡检状态
	 * @param fireTime：巡检时间
	 * @param ip：IP地址
	 * @param planId：任务ID
	 * @param scriptId：脚本ID
	 */
	public void inputResult(int jobId, String status, long fireTime, long nextTime, String planId, String scriptId,
			String scriptName) {
		try {
			PreparedStatement ps = null;
			PreparedStatement ps2 = null;
			Connection conn = null;
			String sql = "";
			dbSingleton db = dbSingleton.get_instance();
			if (db == null) {
				log.error("oracle connect fail. schedule will exit");
				return;
			}
			conn = db.get_db_conn();
			fireTime = this.parseFireTime(fireTime);
			nextTime = this.parseFireTime(nextTime);
			try {
				if (planId != null && !"".equals(planId) && scriptId != null && !"".equals(scriptId)) {
					sql = "select * from T_INSPECT_TASK_RECORD where PLAN_ID = '" + planId + "' and SCRIPT_ID = '"
							+ scriptId + "'";
					List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
					list = db.db_query_assoc(sql);
					if (list.size() == 0) {
						String recordID = UUID.randomUUID().toString().trim().replaceAll("-", "");
						sql = "insert into T_INSPECT_TASK_RECORD(RECORD_ID,PLAN_ID,SCRIPT_ID,"
								+ "CREATE_TIME,NEXT_TIME,JOB_ID,SCRIPT_NAME) values(?,?,?,?,?,?,?)";
						ps = conn.prepareStatement(sql);
						ps.setString(1, recordID);
						ps.setString(2, planId);
						ps.setString(3, scriptId);
						ps.setTimestamp(4, new java.sql.Timestamp(fireTime));
						ps.setTimestamp(5, new java.sql.Timestamp(nextTime));
						ps.setInt(6, jobId);
						ps.setString(7, scriptName);

					} else {
						sql = "update T_INSPECT_TASK_RECORD set JOB_ID=?,CREATE_TIME=?,NEXT_TIME=?"
								+ " where PLAN_ID=? and SCRIPT_ID=? and SCRIPT_NAME=?";
						ps = conn.prepareStatement(sql);
						ps.setInt(1, jobId);
						ps.setTimestamp(2, new java.sql.Timestamp(fireTime));
						ps.setTimestamp(3, new java.sql.Timestamp(nextTime));
						ps.setString(4, planId);
						ps.setString(5, scriptId);
						ps.setString(6, scriptName);
					}
					ps.execute();
					String recordHisID = UUID.randomUUID().toString().trim().replaceAll("-", "");
					sql = "insert into T_INSPECT_TASK_RECORD_HIS(RECORD_ID,PLAN_ID,SCRIPT_ID,"
							+ "CREATE_TIME,NEXT_TIME,JOB_ID,SCRIPT_NAME) values(?,?,?,?,?,?,?)";
					ps2 = conn.prepareStatement(sql);
					ps2.setString(1, recordHisID);
					ps2.setString(2, planId);
					ps2.setString(3, scriptId);
					ps2.setTimestamp(4, new java.sql.Timestamp(fireTime));
					ps2.setTimestamp(5, new java.sql.Timestamp(nextTime));
					ps2.setInt(6, jobId);
					ps2.setString(7, scriptName);
					ps2.execute();
					conn.commit();
				} else {
					log.error("不能确认是那一个任务");
				}
			} catch (SQLException e) {
				log.error("记录巡检结果时异常", e);
			} catch (Exception e) {
				log.error("记录巡检结果异常", e);
			} finally {
				try {
					if (ps != null)
						ps.close();
					if (ps2 != null)
						ps2.close();
				} catch (SQLException e) {
					log.error("蓝鲸-记录巡检结果时异常", e);
				}
			}
		} catch (Exception e) {
			log.error("蓝鲸-解析查询脚本作业结果出错");
			e.printStackTrace();
		}

	}

	public void save(int jobId, String status, long fireTime, long nextTime, String planId, String scriptId) {

	}

	public Long parseFireTime(long fireTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		Date beforeDate = new Date(fireTime);
		String beforeString = sdf.format(beforeDate);
		String afterString = beforeString.replaceAll("\\d+\\.\\d+", "00.000");
		try {
			Date afterDate = sdf.parse(afterString);
			fireTime = afterDate.getTime();
		} catch (ParseException e) {
			log.error("转换fire time异常", e);
		}
		return fireTime;
	}
}
